package xyz.kongbai121.shop.order.web;

import com.alipay.api.AlipayApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.kongbai121.shop.order.config.AlipayTemplate;
import xyz.kongbai121.shop.order.service.OrderService;
import xyz.kongbai121.shop.order.vo.PayVo;

@Controller
public class PayWebController {

    @Autowired
    AlipayTemplate alipayTemplate;

    @Autowired
    OrderService orderService;

    @ResponseBody
    @GetMapping(value = "/payOrder", produces = "text/html")
    public String payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
//        PayVo payVo = new PayVo();
//        payVo.setBody();// 订单备注
//        payVo.setOut_trade_no();// 订单号
//        payVo.setSubject();// 订单主题
//        payVo.setTotal_amount();// 金额
        PayVo payVo = orderService.getOrderPay(orderSn);
        String pay = alipayTemplate.pay(payVo);
        System.out.println(pay);
        return pay;
    }

    /**
     * 模拟订单支付
     */
    @GetMapping("/Payed/{orderSn}")
    public String Payed(@PathVariable("orderSn") String orderSn){
        orderService.handlePayResult(orderSn);
        return "redirect:http://member.gulimall.com/memberOrder.html";
    }
}
